package com.app.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.app.model.Fw;
import com.app.model.FwArea;
import com.app.model.FwStreet;
import com.app.service.IFwAreaService;
import com.app.service.IFwService;
import com.app.service.IFwStreetService;
import com.app.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 房屋片区 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2025-02-27
 */
@Api(tags="房屋片区管理")
@RestController
@RequestMapping("/fwArea")
public class FwAreaController {

    @Autowired
    private IFwAreaService fwAreaService;


    @ApiOperation(value = "获取房屋片区列表", notes = "获取房屋片区列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "keyword", value = "搜索关键字", required = false, paramType = "query", dataType = "string")
    })
    @GetMapping("/getAreaList")
    public Result getAreaList(Integer page, Integer limit,String keyword) {
        Page<FwArea> pageList = new Page<>(page, limit);
        QueryWrapper<FwArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        if(keyword!= null && !keyword.equals("")){
            queryWrapper.like("area_name", keyword);
        }
        queryWrapper.orderByAsc("create_time");

        Page<FwArea> pageInfo = fwAreaService.page(pageList, queryWrapper);
        JSONObject result = new JSONObject();
        result.put("list", pageInfo.getRecords());
        result.put("total", pageInfo.getTotal());
        return Result.success(result);
    }

    @ApiOperation(value = "保存房屋片区信息", notes = "保存房屋片区信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fwArea", value = "房屋片区对象", required = true, paramType = "body", dataType = "FwArea")
    })
    @PostMapping("/saveArea")
    public Result saveArea(@RequestBody FwArea fwArea) {
        Integer loginUserId = StpUtil.getLoginIdAsInt();
        if (fwArea.getId() == null){
            fwArea.setStatus(1);
            fwArea.setCreateId(loginUserId);
            fwArea.setCreateTime(LocalDateTime.now());
            fwAreaService.save(fwArea);
        }else {
            fwArea.setUpdateId(loginUserId);
            fwArea.setUpdateTime(LocalDateTime.now());
            fwAreaService.updateById(fwArea);
        }
        return Result.success("操作成功！");
    }


    @Autowired
    private IFwStreetService fwStreetService;

    @Autowired
    private IFwService fwService;


    @ApiOperation(value = "同步房屋片区信息", notes = "同步房屋片区信息")
    @GetMapping("/synchronizationFwArea")
    public void synchronizationFwArea() {
        QueryWrapper<FwStreet> queryWrapper = new QueryWrapper<>();
        List<FwStreet> fwStreetList = fwStreetService.list(queryWrapper);
        for (FwStreet fwStreet : fwStreetList){
            String street_name = fwStreet.getStreetName();

            QueryWrapper<Fw> fwQueryWrapper = new QueryWrapper<>();
            fwQueryWrapper.eq("street_name", street_name);
            fwQueryWrapper.last("LIMIT 1");
            Fw fw = fwService.getOne(fwQueryWrapper);

            QueryWrapper<FwArea> fwAreaQueryWrapper = new QueryWrapper<>();
            fwAreaQueryWrapper.eq("area_name", fw.getAreaName());
            fwAreaQueryWrapper.last("LIMIT 1");
            FwArea fwArea = fwAreaService.getOne(fwAreaQueryWrapper);

            if (fwArea != null){
                fwStreet.setAreaId(fwArea.getId());
            }else {
                FwArea newFwArea = new FwArea();
                newFwArea.setAreaName(fw.getAreaName());
                newFwArea.setStatus(1);
                newFwArea.setCreateId(StpUtil.getLoginIdAsInt());
                newFwArea.setCreateTime(LocalDateTime.now());
                fwAreaService.save(newFwArea);
                fwStreet.setAreaId(newFwArea.getId());
            }
            fwStreetService.updateById(fwStreet);

        }
    }

}

